package com.fractalwrench.harmonybootstrap.sample;

import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.DANGER;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.INFO;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.PRIMARY;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.REGULAR;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.SECONDARY;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.SUCCESS;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.WARNING;

import com.beardedhen.harmonybootstrap.BootstrapThumbnail;
import com.beardedhen.harmonybootstrap.utils.ResUtil;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapSize;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.PixelMapElement;

public class BootstrapThumbnailExample extends AbilitySlice {

    private BootstrapThumbnail imageChange, themeChange, borderChange, roundedChange, sizeChange, setPixelMapObj,
        setImageElement, setPixelMapRes;

    private int resId = ResourceTable.Media_ladybird;

    private DefaultBootstrapSize size = DefaultBootstrapSize.MD;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_example_bootstrap_thumbnail, null, false);
        super.setUIContent(rootLayout);
        imageChange = (BootstrapThumbnail) rootLayout.findComponentById(ResourceTable.Id_bthumb_image_change_example);
        themeChange = (BootstrapThumbnail) rootLayout.findComponentById(ResourceTable.Id_bthumb_theme_change_example);
        borderChange = (BootstrapThumbnail) rootLayout.findComponentById(ResourceTable.Id_bthumb_border_change_example);
        roundedChange = (BootstrapThumbnail) rootLayout.findComponentById(
            ResourceTable.Id_bthumb_rounded_change_example);
        sizeChange = (BootstrapThumbnail) rootLayout.findComponentById(ResourceTable.Id_bthumb_size_change_example);
        setPixelMapObj = (BootstrapThumbnail) rootLayout.findComponentById(
            ResourceTable.Id_bthumb_set_pixelmap_pixelmap_example);
        setImageElement = (BootstrapThumbnail) rootLayout.findComponentById(
            ResourceTable.Id_bthumb_set_image_element_example);
        setPixelMapRes = (BootstrapThumbnail) rootLayout.findComponentById(
            ResourceTable.Id_bthumb_set_pixelmap_resource_example);

        if (setPixelMapObj != null) {
            setPixelMapObj.setPixelMap(ResUtil.getPixelMap(this, ResourceTable.Media_small_daffodils).get());
        }

        if (setImageElement != null) {
            setImageElement.setImageElement(new PixelMapElement(ResUtil.getPixelMapScaled(this, resId).get()));
        }
        if (setPixelMapRes != null) {
            setPixelMapRes.setPixelMap(ResourceTable.Media_caterpillar);
        }
        if (themeChange != null) {
            //Tap to change theme
            themeChange.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    switch ((DefaultBootstrapBrand) themeChange.getBootstrapBrand()) {
                        case PRIMARY:
                            themeChange.setBootstrapBrand(SUCCESS);
                            break;
                        case SUCCESS:
                            themeChange.setBootstrapBrand(INFO);
                            break;
                        case INFO:
                            themeChange.setBootstrapBrand(WARNING);
                            break;
                        case WARNING:
                            themeChange.setBootstrapBrand(DANGER);
                            break;
                        case DANGER:
                            themeChange.setBootstrapBrand(SECONDARY);
                            break;
                        case SECONDARY:
                            themeChange.setBootstrapBrand(REGULAR);
                            break;
                        case REGULAR:
                            themeChange.setBootstrapBrand(PRIMARY);
                            break;
                    }
                }
            });
        }
        if (imageChange != null) {
            //Tap to change image
            imageChange.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    switch (resId) {
                        case ResourceTable.Media_ladybird:
                            resId = ResourceTable.Media_caterpillar;
                            break;
                        case ResourceTable.Media_caterpillar:
                            resId = 0;
                            break;
                        case 0:
                            resId = ResourceTable.Media_ladybird;
                            break;
                    }
                    imageChange.setPixelMap(resId);
                }
            });
        }
        if (roundedChange != null) {
            //Tap to change Rounded
            roundedChange.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    roundedChange.setRounded(!roundedChange.isRounded());
                }
            });
        }
        if (borderChange != null) {
            //Tap to change the border
            borderChange.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    borderChange.setBorderDisplayed(!borderChange.isBorderDisplayed());
                }
            });
        }
        if (sizeChange != null) {
            sizeChange.setLayoutConfig(getLayoutParams(size.scaleFactor()));
            //Tap to change the Size
            sizeChange.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    switch (size) {
                        case XS:
                            size = DefaultBootstrapSize.SM;
                            break;
                        case SM:
                            size = DefaultBootstrapSize.MD;
                            break;
                        case MD:
                            size = DefaultBootstrapSize.LG;
                            break;
                        case LG:
                            size = DefaultBootstrapSize.XL;
                            break;
                        case XL:
                            size = DefaultBootstrapSize.XS;
                            break;
                    }
                    sizeChange.setBootstrapSize(size);
                    sizeChange.setLayoutConfig(getLayoutParams(size.scaleFactor()));
                }
            });
        }
    }

    private DirectionalLayout.LayoutConfig getLayoutParams(float factor) {
        float baselineSize = 300;
        float size = baselineSize * factor;
        return new DirectionalLayout.LayoutConfig((int) size, (int) size);
    }
}
